<?php
/**
 * 富友银行对接接口类
 * User: LuoChen Lan
 * Date: 2017/4/26
 * Time: 13:35
 */

namespace Business\Fuyou;

class Fuyou {
    public $config = null;
    function __construct() {
        $this->config = parse_ini_file(HTML_DIR . '/extend/Business/Fuyou/fuYouConfig.ini', true);
    }

    /**
     * 账单支付方法
     * @date   2017-04-26
     * @author LuoChen Lan
     *
     * @param  string  $orderId  订单号
     * @param  int     $money    支付金额
     * @param  string  $bankType 银行类型
     * @param  string  $url      同步通知地址
     * @param  int     $payType  支付方式 0=B2C  1=B2B
     *
     * @return bool
     */
    public function pay($orderId, $money, $bankType, $url, $payType = 0) {
        if ($payType == 0) {
            $orderPayType = 'B2C';
            $backUrl = $this->config[ENV]['backNotifyUrlB2C'];
        } else {
            $orderPayType = 'B2B';
            $backUrl = $this->config[ENV]['backNotifyUrlB2B'];
        }
        $paramArr = [
            'cur_type'          => 'CNY',
            'mchnt_cd'          => $this->config[ENV]['mchntId'],
            'order_id'          => $orderId,
            'order_amt'         => $money,
            'order_pay_type'    => $orderPayType,
            'page_notify_url'   => $url,
            'back_notify_url'   => $backUrl,
            'order_valid_time'  => $this->config[ENV]['orderValidTime'],
            'iss_ins_cd'        => $bankType,
            'goods_name'        => '租付通账单缴费',
            'goods_display_url' => '',
            'rem'               => '租赁账单缴费',
            'rcvr'              => '保留字段',
            'ver'               => $this->config[ENV]['version'],
            'mchntKey'          => $this->config[ENV]['mchntKey'],
        ];

        $paramArr['md5'] = md5($this->FuYouStr($paramArr));
        $paramArr['mchnt_ssn'] = $orderId;
        zft_log('fuyou/pay', json_encode($paramArr));
        unset($paramArr['mchntKey']);
        $this->showHtml($paramArr);
    }

    /**
     * form表单组织
     * @date   2017-04-26
     * @author LuoChen Lan submitForm()
     *
     * @param  array  $paramArr  表单数据数组
     *
     * @return none
     */
     protected function showHtml($paramArr) {
        $htmlStr = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                    <title>提交到富友交易系统</title>
                </head>
            <script type="text/javascript">
                function submitForm(){
                    document.getElementById("form").submit();
                }
            </script>
        <body onload="javascript:submitForm();">
	        <form name="pay" method="post" action="'.$this->config[ENV]['url'].'" id = "form">
        ';
        foreach ($paramArr as $key => $val) {
            $htmlStr .= '<input type="hidden" value = "'.$this->nvl($val).'" name="'.$this->nvl($key).'"/>';
        }
        $htmlStr .= ' </form></body>';
        echo $htmlStr;
        die;
    }

    /**
     * 富友加密处理
     * @date   2017-04
     * @author LuoChen Lan
     *
     * @param  array  $param  加密数据
     *
     * @return string
     */
     public function FuYouStr($param) {
        if (!is_array($param)) {
            return $param;
        }
        $str = '';
        foreach ($param as $val) {
            $str .= '|'.$val;
        }
        $str = ltrim($str, '|');
        return $str;
    }

    /**
     * 富友参数不许为null
     * @date   2017-04
     * @author LuoChen Lan
     *
     * @param  string  $param  判断的参数
     *
     * @return
     */
    public function nvl($param) {
        return  $param == null ? '' : $param;
    }
}